/*
 * addon_entry.cpp
 *
 *  Created on: 2010-1-7
 *      Author: gavinkwoe
 */

#include "addon_features.h"

#if (__ADDON_TARGET__ == __TARGET_SYMBIAN)

#include "addon_std.h"
#include "addon_mem.h"
#include "addon_objbase.h"
#include "BookCityViewObserver.h"

/*************************************************************************
* GUID
*************************************************************************/

ADDON_DEFINED_GUID( IAddonBookCityObserver, 1, "E374239D-ADFA-4d10-A446-BDDFC26C6E0D" )

/*************************************************************************
* STRUCTURE
*************************************************************************/

struct __struct_addon_bookcity_Observer_data;
typedef struct __struct_addon_bookcity_Observer_data		addon_bookcity_Observer_data_t;
typedef struct __struct_addon_bookcity_Observer_data *	addon_bookcity_Observer_data_ptr_t;

/*************************************************************************
* DEFINITION
*************************************************************************/

struct __struct_addon_bookcity_Observer_data
{
	unsigned long		        ref_cnt;	// reference count
	addon_obsvr_fptr_bookcity_t	ntf_hdlr;
	addon_obsvr_fptr_bookcity_t	ntf_downhdlr;
	addon_obsvr_fptr_bookcity_t ntf_updatehdlr;
	unsigned long		        has_ud;	
	void *				        user_data;
};

/*************************************************************************
* LOCAL FUNCTIONS
*************************************************************************/

//const unsigned long	__CBookcity_Observer_Construct( ADDON_REF(IObject) thiz, void * usr_data );
unsigned long	__CBookcity_Observer_Destruct( ADDON_REF(IObject) thiz );
unsigned long	__CBookcity_Observer_AddRef( ADDON_REF(IObject) thiz );
unsigned long	__CBookcity_Observer_Release( ADDON_REF(IObject) thiz );

unsigned long	__CBookcity_Observer_GetUserData( ADDON_REF(IObject) thiz, void ** pusr_data );
unsigned long	__CBookcity_Observer_SetUserData( ADDON_REF(IObject) thiz, void * usr_data );
unsigned long	__CBookcity_Observer_SetCallback( ADDON_REF(IObject) thiz, addon_obsvr_fptr_bookcity_t hdlr );
unsigned long	__CBookcity_Observer_SendNotify( ADDON_REF(IObject) thiz, int ntf_status, void * ntf_data );

unsigned long	__CBookcity_SetDownloadCallback( ADDON_REF(IObject) thiz, IN addon_obsvr_fptr_bookcity_t hdlr );
unsigned long	__CBookcity_SendDownloadNotify( ADDON_REF(IObject) thiz, IN int ntf_status, IN void * ntf_data );


// const void * const __coclass_vtbl[] =
// {
// 	// IObject
// 	(void *)__CBookcity_Observer_Construct,
// 		(void *)__CBookcity_Observer_Destruct,
// 		(void *)__CBookcity_Observer_AddRef,
// 		(void *)__CBookcity_Observer_Release,
// 		
// 		(void *)__CBookcity_Observer_GetUserData,
// 		(void *)__CBookcity_Observer_SetUserData,
// 		(void *)__CBookcity_Observer_SetCallback,
// 		(void *)__CBookcity_Observer_SendNotify,
// 		(void *)__CBookcity_SetDownloadCallback,
// 		(void *)__CBookcity_SendDownloadNotify
// };
// 
// const addon_coclass_desc_t __coclass_desc =
// {
// 	(unsigned char *)__coclass_vtbl,
// 		(unsigned long)sizeof( addon_bookcity_Observer_data_t )
// };

/*************************************************************************
* IMPLEMENTATION
*************************************************************************/

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
const unsigned long __CBookcity_Observer_Construct( ADDON_REF(IObject) thiz, void * usr_data )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	ADDON_UNUSED( usr_data );
	
	if ( NULL == obj_data )
		return ADDON_FAILED;

	obj_data->ref_cnt	= 1;
	obj_data->ntf_hdlr	= NULL;
	obj_data->ntf_downhdlr	= NULL;
	obj_data->ntf_updatehdlr = NULL;
	obj_data->has_ud	= ADDON_FALSE;
	obj_data->user_data	= NULL;

	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CBookcity_Observer_Destruct( ADDON_REF(IObject) thiz )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;

	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CBookcity_Observer_AddRef( ADDON_REF(IObject) thiz )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	obj_data->ref_cnt++;
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CBookcity_Observer_Release( ADDON_REF(IObject) thiz )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	obj_data->ref_cnt -= 1;

	if ( 0 == obj_data->ref_cnt )
	{
		addon_obj_delete( (void *)thiz );
	}
	
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CBookcity_Observer_GetUserData( ADDON_REF(IObject) thiz, void ** pusr_data )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data || NULL == pusr_data )
		return ADDON_FAILED;
	
	if ( ADDON_FALSE == obj_data->has_ud )
		return ADDON_FAILED;
	
	*pusr_data = obj_data->user_data;
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CBookcity_Observer_SetUserData( ADDON_REF(IObject) thiz, void * usr_data )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	obj_data->has_ud	= ADDON_TRUE;
	obj_data->user_data	= usr_data;
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CBookcity_Observer_SetCallback( ADDON_REF(IObject) thiz, addon_obsvr_fptr_bookcity_t hdlr )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	obj_data->ntf_hdlr = hdlr;
	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
unsigned long __CBookcity_Observer_SendNotify( ADDON_REF(IObject) thiz, int ntf_status, void * ntf_data )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;
	
	if ( NULL != obj_data->ntf_hdlr )
	{
		ADDON_INVOKE( obj_data->ntf_hdlr, ( thiz, ntf_status, ntf_data ) );
	}
	
	return ADDON_OK;
}

unsigned long	__CBookcity_SetDownloadCallback( ADDON_REF(IObject) thiz, IN addon_obsvr_fptr_bookcity_t hdlr )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;

	obj_data->ntf_downhdlr = hdlr;
	return ADDON_OK;
}

unsigned long	__CBookcity_SendDownloadNotify( ADDON_REF(IObject) thiz, IN int ntf_status, IN void * ntf_data )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;

	if ( NULL != obj_data->ntf_downhdlr )
	{
		ADDON_INVOKE( obj_data->ntf_downhdlr, ( thiz, ntf_status, ntf_data ) );
	}

	return ADDON_OK;
}

static unsigned long	__CBookcity_SetUpdateCallback( ADDON_REF(IObject) thiz, IN addon_obsvr_fptr_bookcity_t hdlr )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;

	obj_data->ntf_updatehdlr = hdlr;
	return ADDON_OK;
}

static unsigned long	__CBookcity_SendUpdateNotify( ADDON_REF(IObject) thiz, IN int ntf_status, IN void * ntf_data )
{
	addon_bookcity_Observer_data_ptr_t obj_data = (addon_bookcity_Observer_data_ptr_t)ADDON_GET_DATA( thiz );

	if ( NULL == obj_data )
		return ADDON_FAILED;

	if ( NULL != obj_data->ntf_updatehdlr )
	{
		ADDON_INVOKE( obj_data->ntf_updatehdlr, ( thiz, ntf_status, ntf_data ) );
	}

	return ADDON_OK;
}

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
ADDON_EXPORT ADDON_REF(IObject) addon_create_bookcity_observer( void * reserved )
{
	//static const 
// 	const void * const __coclass_vtbl[10] =
// 	{
// 		// IObject
// 		(void *)__CBookcity_Observer_Construct,
// 		(void *)__CBookcity_Observer_Destruct,
// 		(void *)__CBookcity_Observer_AddRef,
// 		(void *)__CBookcity_Observer_Release,
// 
// 		(void *)__CBookcity_Observer_GetUserData,
// 		(void *)__CBookcity_Observer_SetUserData,
// 		(void *)__CBookcity_Observer_SetCallback,
// 		(void *)__CBookcity_Observer_SendNotify,
// 		(void *)__CBookcity_SetDownloadCallback,
// 		(void *)__CBookcity_SendDownloadNotify
// 	};
	void * __coclass_vtbl[12];
	int i=0;
	__coclass_vtbl[i++] =(void *)__CBookcity_Observer_Construct;
	__coclass_vtbl[i++] =(void *)__CBookcity_Observer_Destruct;
	__coclass_vtbl[i++] =(void *)__CBookcity_Observer_AddRef;
	__coclass_vtbl[i++] =(void *)__CBookcity_Observer_Release;
	__coclass_vtbl[i++] =(void *)__CBookcity_Observer_GetUserData;
	__coclass_vtbl[i++] =(void *)__CBookcity_Observer_SetUserData;
	__coclass_vtbl[i++] =(void *)__CBookcity_Observer_SetCallback;
	__coclass_vtbl[i++] =(void *)__CBookcity_Observer_SendNotify;
	__coclass_vtbl[i++] =(void *)__CBookcity_SetDownloadCallback;
	__coclass_vtbl[i++] =(void *)__CBookcity_SendDownloadNotify;
	__coclass_vtbl[i++] =(void *)__CBookcity_SetUpdateCallback;
	__coclass_vtbl[i++] =(void *)__CBookcity_SendUpdateNotify;
// 	{
// 		// IObject
// 		(void *)__CBookcity_Observer_Construct,
// 			(void *)__CBookcity_Observer_Destruct,
// 			(void *)__CBookcity_Observer_AddRef,
// 			(void *)__CBookcity_Observer_Release,
// 			
// 			(void *)__CBookcity_Observer_GetUserData,
// 			(void *)__CBookcity_Observer_SetUserData,
// 			(void *)__CBookcity_Observer_SetCallback,
// 			(void *)__CBookcity_Observer_SendNotify,
// 			(void *)__CBookcity_SetDownloadCallback,
// 			(void *)__CBookcity_SendDownloadNotify
// 	};

	
// 	static const addon_coclass_desc_t __coclass_desc =
// 	{
// 		(unsigned char *)__coclass_vtbl,
// 		(unsigned long)sizeof( addon_bookcity_Observer_data_t )
// 	};
		addon_coclass_desc_t __coclass_desc;
		
		__coclass_desc.vtbl_size	= (unsigned long)sizeof(__coclass_vtbl);
		__coclass_desc.vtbl_funcs	= (unsigned char *)__coclass_vtbl;
		__coclass_desc.priv_size	= (unsigned long)sizeof(addon_bookcity_Observer_data_t);
		


	return ( ADDON_REF(IObject) )addon_obj_new( (addon_coclass_desc_ptr_t)&__coclass_desc, reserved );
}

#else	// #if (__ADDON_TARGET__ == __TARGET_SYMBIAN)

/*************************************************************************
* DESCRIPTION
*	$$__FUNC_DESC__$$
*************************************************************************/
ADDON_EXPORT ADDON_REF(IObject) addon_create_observer( void * reserved )
{
	ADDON_UNUSED( reserved );
	return NULL;
}

#endif	// #if (__ADDON_TARGET__ == __TARGET_SYMBIAN)
